package tour.spring.web.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import tour.spring.bc.bo.impl.ReservationBoImpl;
import tour.spring.bc.bo.impl.TourBoImpl;
import tour.spring.bc.dao.TourDao;
import tour.spring.bc.dto.HotelDto;
import tour.spring.bc.dto.TourDto;
import tour.spring.bc.enums.TourLength;
import tour.spring.bc.model.Tour;
import tour.spring.bc.params.TourSearchParams;
import tour.spring.web.commandobject.BookTourForm;

@Controller
public class TourController {

	static final Logger logger = Logger.getLogger(TourController.class);
	@Autowired
	TourBoImpl tourBo;

	@Autowired
	ReservationBoImpl reservationBo;
	
	@InitBinder
	public void initBinder(WebDataBinder binder) {

		SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
	}

	@RequestMapping(value = "/queryTours.html", method = RequestMethod.GET)
	public String getAllTours(HttpServletRequest request, ModelMap model) {

		Collection<TourDto> tours = tourBo.queryAll();
		model.addAttribute("toursCollection", tours);
		return "/queryToursResult";
	}

	@RequestMapping(value = "/searchTours.html", method = RequestMethod.GET)
	public String getSearchToursPage(HttpServletRequest request, ModelMap model) {

		logger.debug("-_---------------------> conntroler");
		
		Collection<TourDto> tours = tourBo.queryAll();

		Set<HotelDto> hotels = new HashSet<HotelDto>();
		for (TourDto tourDto : tours) {
			hotels.add(tourDto.getHotelDto());
		}

		model.addAttribute("hotels", hotels);

		List<TourLength> tourLengthList = new ArrayList<TourLength>();
		for (TourLength tourLength : TourLength.values()) {
			tourLengthList.add(tourLength);
		}
		model.addAttribute("tourLengthList", tourLengthList);

		model.addAttribute("tourForm", new TourSearchParams());

		return "/searchTours";
	}

	@RequestMapping(value = "/searchToursResult.html", method = RequestMethod.POST)
	public String getSearchToursResult(@ModelAttribute("tourForm") TourSearchParams tourForm,
			ModelMap model) {

		Collection<TourDto> tours = tourBo.searchTours(tourForm);

		model.addAttribute("tours", tours);
		return "/findToursResult";
	}

	@RequestMapping(value = "user/reservationTourPage.html", method = RequestMethod.GET)
	public String getReservationTourPage(@RequestParam(value = "tourId", required = true) long tourId,
			ModelMap model) {
		TourDto tour = tourBo.loadById(tourId);
		List<String> imgPaths = tourBo.loadPathImageByTourId(tourId);
		String description = tourBo.loadTourDescriptionById(tourId);

		model.addAttribute("tour", tour);
		model.addAttribute("imgPaths", imgPaths);
		model.addAttribute("description", description);
		
		BookTourForm bookTourForm = new BookTourForm();
		bookTourForm.setTourId(tourId);
		model.addAttribute("bookTourForm", bookTourForm);

		return "user/reservationTourPage";
	}

	@RequestMapping(value = "user/reservationTour.html", method = RequestMethod.POST)
	public String reservationTour(@ModelAttribute(value = "bookTourForm") BookTourForm bookTourForm, ModelMap model) {

		Authentication auth = SecurityContextHolder.getContext().getAuthentication();
		String clientLogin = auth.getName(); // get logged in username
		System.out.println("---> " + bookTourForm.getTourId());
		reservationBo.saveReservation(bookTourForm.getTourId(), clientLogin, bookTourForm.getParticipantQuantity());
		
		return "user/reservationTourComplete";
	}
	
	@RequestMapping(value = "reservationTour.html", method = RequestMethod.GET)
	public String testReservationTour(ModelMap model) {
		return "user/reservationTourComplete";
	}

	@RequestMapping(value = "/tourDetail.html", method = RequestMethod.POST)
	public @ResponseBody String getTourDetails(@RequestParam(value = "tourId", required = true) Long tourId, Model model) {

		String tourDetails = tourBo.loadTourDescriptionById(tourId);
		return tourDetails;
	}

	@RequestMapping(value = "/tourImages.html", method = RequestMethod.POST)
	public @ResponseBody List<String> getTourImages(@RequestParam(value = "tourId", required = true) Long tourId, Model model) {

		List<String> paths = tourBo.loadPathImageByTourId(tourId);
		return paths;
	}

	@RequestMapping(value = "user/testuser.html", method = RequestMethod.GET)
	public String getTestUser(ModelMap model) {
		return "user/testuser";
	}

	@RequestMapping(value = "user/testuser2.html", method = RequestMethod.GET)
	public String getTestUser2(ModelMap model) {
		return "user/testuser2";
	}

	public TourBoImpl getTourBo() {
		return tourBo;
	}

	public void setTourBo(TourBoImpl tourBo) {
		this.tourBo = tourBo;
	}

	public ReservationBoImpl getReservationBo() {
		return reservationBo;
	}

	public void setReservationBo(ReservationBoImpl reservationBo) {
		this.reservationBo = reservationBo;
	}

}
